﻿using System;
using System.Linq;
using System.Management;
using System.Net.Sockets;
using System.Net;
using System.ServiceModel;
using ET.CommonLibrary.Log;
using ET.HeartBeat.SharedLibrary;
using ET.Score.Service;
using ET.Score.Service.Enums;
using ET.Score.Service.Interfaces;

namespace ET.Score.SharedLibrary.Wcf
{
    public class HeartMonitorProvider : HeartBeatInvoker<IHeartBeatService>
    {
        private readonly string endpointName;

        public static DuplexChannelFactory<IHeartBeatService> CreateChannelFactory(string endpointName = null, IHeartBeatCallback callbackObj = null)
        {
            if (endpointName == null)
                endpointName = typeof(HeartMonitorProvider).FullName;

            if (callbackObj == null)
                callbackObj = new HeartBeatCallBack();

            var factory = new DuplexChannelFactory<IHeartBeatService>(callbackObj, endpointName);
            return factory;
        }

        private static WcfMachineInfo reportInfo;
        public static string UserName = string.Empty;

        public static bool HeartBeat(IHeartBeatService proxy,bool Flag=false)
        {
            try
            {
                if(reportInfo==null)
                {
                    reportInfo = GetMachineInfo();
                }
                if (UserName != string.Empty)
                    reportInfo.UserName = UserName;
                proxy.HeartMonitor(reportInfo, Flag);
            }
            catch(Exception EX)
            {

            }
            return true;
        }

        public HeartMonitorProvider(IHeartBeatCallback callbackObj = null, string endpointName = null)
            : base(CreateChannelFactory(endpointName, callbackObj), HeartBeat)
        {
            this.endpointName = endpointName;
            EnvironmentContext.Logger.Info("心跳上报信息已读取:Id:{0}, type:{1}", reportInfo.MachineIP, reportInfo.MachineType);
        }

        private static WcfMachineInfo GetMachineInfo()
        {
            return new WcfMachineInfo()
            {
                SerialNumber= GetUniqueIdentifier(),
                MachineIP = GetLocalIPv4Address(),
                MachineType = LoginType.InvalidType,
                UserName = "TEST"//EnvironmentContext.AppSettings["UserName"]
            };
        }

        public static string GetUniqueIdentifier()
        {
            string identifier = "";
            identifier=MotherboardInfo.GetMotherboardSerial();
            return identifier;
        }
        /// <summary>
        /// 获取本机的第一个IPV4地址
        /// </summary>
        /// <returns></returns>
        public static string GetLocalIPv4Address()
        {
            string ipAddress = String.Empty;
            IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());

            foreach (IPAddress ip in host.AddressList)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    ipAddress = ip.ToString();
                    break;
                }
            }
            return ipAddress;
        }
    }
}

